home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / FindDialogorig.java < prev    next >
Text File  |  1998-10-30  |  6KB  |  207 lines

  1. /*
  2.     A basic extension of the java.awt.Dialog class
  3.  */
  4.  
  5. import java.awt.*;
  6.  
  7. public class FindDialog extends Dialog {
  8.     void cancelButton_Clicked(java.awt.event.ActionEvent event) {
  9.         // to do: place event handler code here.
  10.         statuslabel.setText("");
  11.         dispose();
  12.         dispose();
  13.     }
  14.  
  15.     void findButton_Clicked(java.awt.event.ActionEvent event, boolean has_dialog) {
  16.  
  17.         int suspect_length;
  18.         int foundit_index;
  19.         String found_string;
  20.  
  21.         m_findme = findText.getText();
  22.         suspect_length = m_findme.length();
  23.         foundit_index = m_body_string.indexOf(m_findme, m_current_index);
  24.         if(foundit_index == -1) {
  25.             if(has_dialog) {
  26.                 statuslabel.setText("End of File");
  27.                 statuslabel.setVisible(true);
  28.             }
  29.         }
  30.         else {
  31.             if(m_MatchCase.getState()) {
  32.                 found_string = m_body_string.substring(foundit_index, foundit_index + suspect_length);
  33.                 if(found_string.equals(m_findme)) {
  34.                     if(has_dialog) {
  35.                         statuslabel.setText("Found");
  36.                         statuslabel.setVisible(true);
  37.                     }
  38.                     m_body.select(foundit_index, foundit_index + suspect_length);
  39.                     m_current_index = foundit_index + suspect_length;
  40.                 }
  41.             }
  42.                else {
  43.                 statuslabel.setText("Found");
  44.                 statuslabel.setVisible(true);
  45.                 m_body.select(foundit_index, foundit_index + suspect_length);
  46.                 m_current_index = foundit_index + suspect_length;
  47.             }
  48.         }
  49.     }
  50.  
  51.  
  52.     public void setBody(TextArea thebody)
  53.     {
  54.         m_body = thebody;
  55.         m_body_string = m_body.getText();
  56.     }
  57.  
  58.     public void setSuspect(String thesuspect)
  59.     {
  60.         m_findme = thesuspect;
  61.         findText.setText(m_findme);
  62.     }
  63.  
  64.     public void setStart(int start)
  65.     {
  66.         m_current_index = start;
  67.     }
  68.  
  69.     public boolean matchCase()
  70.     {
  71.         return m_MatchCase.getState();
  72.     }
  73.  
  74.     public String getFindString()
  75.     {
  76.         return findText.getText();
  77.     }
  78.  
  79.     public void addNotify()
  80.     {
  81.         super.addNotify();
  82.  
  83.         if (fComponentsAdjusted)
  84.             return;
  85.  
  86.         // Adjust components according to the insets
  87.         setSize(insets().left + insets().right + getSize().width, insets().top + insets().bottom + getSize().height);
  88.         Component components[] = getComponents();
  89.         for (int i = 0; i < components.length; i++)
  90.         {
  91.             Point p = components[i].getLocation();
  92.             p.translate(insets().left, insets().top);
  93.             components[i].setLocation(p);
  94.         }
  95.         fComponentsAdjusted = true;
  96.     }
  97.  
  98.     boolean fComponentsAdjusted = false;
  99.  
  100.     public FindDialog(Frame parent, boolean modal) {
  101.  
  102.         super(parent, modal);
  103.  
  104.         //{{INIT_CONTROLS
  105.         setLayout(null);
  106.         setVisible(false);
  107.         setSize(insets().left + insets().right + 297,insets().top + insets().bottom + 98);
  108.         setBackground(new Color(12632256));
  109.         label2 = new java.awt.Label("Find What");
  110.         label2.setBounds(insets().left + 12,insets().top + 12,80,16);
  111.         add(label2);
  112.         findText = new java.awt.TextField();
  113.         findText.setBounds(insets().left + 12,insets().top + 36,190,20);
  114.         add(findText);
  115.         findText.setCursor(new Cursor(Cursor.TEXT_CURSOR));
  116.         findButton = new java.awt.Button();
  117.         findButton.setLabel("Find");
  118.         findButton.setBounds(insets().left + 216,insets().top + 12,70,20);
  119.         add(findButton);
  120.         cancelButton = new java.awt.Button();
  121.         cancelButton.setLabel("Cancel");
  122.         cancelButton.setBounds(insets().left + 216,insets().top + 36,70,20);
  123.         cancelButton.setFont(new Font("Dialog", Font.PLAIN, 12));
  124.         add(cancelButton);
  125.         statuslabel = new java.awt.Label("");
  126.         statuslabel.setVisible(false);
  127.         statuslabel.setBounds(insets().left + 168,insets().top + 60,120,24);
  128.         add(statuslabel);
  129.         m_MatchCase = new java.awt.Checkbox("Match case");
  130.         m_MatchCase.setBounds(insets().left + 12,insets().top + 60,84,20);
  131.         m_MatchCase.setBackground(new Color(12632256));
  132.         add(m_MatchCase);
  133.         setTitle("Find");
  134.         setResizable(false);
  135.         //}}
  136.  
  137.         //{{REGISTER_LISTENERS
  138.         Action lAction = new Action();
  139.         findButton.addActionListener(lAction);
  140.         cancelButton.addActionListener(lAction);
  141.         //}}
  142.     }
  143.  
  144.     public FindDialog(JavaPad parent, String title, boolean modal, String findString) {
  145.         this(parent, modal);
  146.         setTitle(title);
  147.         findText.setText(findString);
  148.     }
  149.  
  150.     public void show() {
  151.         Rectangle bounds = getParent().bounds();
  152.         Rectangle abounds = bounds();
  153.  
  154.         move(bounds.x + (bounds.width - abounds.width)/ 2,
  155.              bounds.y + (bounds.height - abounds.height)/2);
  156.  
  157.         super.show();
  158.     }
  159.  //-------------------------------------------------------------
  160.  // This file has been migrated from the 1.0 to 1.1 event model.
  161.  // This method is not used with the new 1.1 event model. You can
  162.  // move any code you need to keep, then remove this method.
  163.  //-------------------------------------------------------------
  164.  //
  165.  //     public boolean handleEvent(Event event) {
  166.  //         if(event.id == Event.WINDOW_DESTROY) {
  167.  //             hide();
  168.  //             return true;
  169.  //         }
  170.  //         if (event.target == findButton && event.id == Event.ACTION_EVENT) {
  171.  //             findButton_Clicked(event);
  172.  //         }
  173.  //         if (event.target == cancelButton && event.id == Event.ACTION_EVENT) {
  174.  //             cancelButton_Clicked(event);
  175.  //         }
  176.  //         return super.handleEvent(event);
  177.  //     }
  178.  //-------------------------------------------------------------
  179.  
  180.     //{{DECLARE_CONTROLS
  181.     java.awt.Label label2;
  182.     java.awt.TextField findText;
  183.     java.awt.Button findButton;
  184.     java.awt.Button cancelButton;
  185.     java.awt.Label statuslabel;
  186.     java.awt.Checkbox m_MatchCase;
  187.     //}}
  188.  
  189.     TextArea m_body;
  190.     String m_body_string;
  191.     String m_findme;
  192.     int m_current_index;
  193.  
  194.  
  195.     class Action implements java.awt.event.ActionListener
  196.     {
  197.         public void actionPerformed(java.awt.event.ActionEvent event)
  198.         {
  199.             Object object = event.getSource();
  200.             if (object == findButton)
  201.                 findButton_Clicked(event, true);
  202.             else if (object == cancelButton)
  203.                 cancelButton_Clicked(event);
  204.         }
  205.     }
  206. }
  207.